package com.zkhz.base.util.system;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.stereotype.Component;

/**
 * @Description 简单获取Bean实例的工具，也可以用来发布事件{@link #publishEvent(ApplicationEvent)}
 *
 * @author wuzeqin
 * @date 2020/5/18
 **/
@Component
public class SpringBeansUtil {

    private static ApplicationContext CONTEXT;

    public SpringBeansUtil(ApplicationContext applicationContext) {
        CONTEXT = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return CONTEXT;
    }

    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }

    public static void publishEvent(ApplicationEvent event) {
        CONTEXT.publishEvent(event);
    }

    public static void publishEvent(Object event) {
        CONTEXT.publishEvent(event);
    }
}
